#ifndef UTILITY_H
#define UTILITY_H

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <glut.h>
#include <glui.h>
#include "Temp.h"

static GLint    ImageWidth=600;
static GLint    ImageHeight=600;


float obj_rot[16];

GLfloat light_position[4]={0.0, 0.0, 3.0, 1.0};

GLfloat eye_position[4]={0,0,-9,1};

GLUI *glui;

Cloth model;

void InitGL();
void DrawCloth();
void InitCloth();

void InitGL()
{
	//glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	//glEnable(GL_LIGHTING);
	//glEnable(GL_LIGHT0);
	glDepthFunc(GL_LESS);
	glEnable(GL_DEPTH_TEST);
	glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

	glViewport(0, 0, (GLsizei) ImageWidth, (GLsizei) ImageHeight);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f,(GLfloat) ImageWidth / (GLfloat)ImageHeight , 0.5f, -1000.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void DrawCloth()
{
	glBegin(GL_TRIANGLES);

	for (int i=0;i<model.triangles.size();i++)
	{
		Triangle* tri=model.triangles.at(i);

		Vertex *v1, *v2, *v3;

		v1=tri->vertices[0];
		v2=tri->vertices[1];
		v3=tri->vertices[2];

		//glNormal3f(v1->normal.x,v1->normal.y,v1->normal.z);
		glVertex3f(v1->position.x,v1->position.y,v1->position.z);

		//glNormal3f(v2->normal.x,v2->normal.y,v2->normal.z);
		glVertex3f(v2->position.x,v2->position.y,v2->position.z);

		//glNormal3f(v3->normal.x,v3->normal.y,v3->normal.z);
		glVertex3f(v3->position.x,v3->position.y,v3->position.z);
	}

	glEnd();

	model.UpdateCloth(0.1);

	/*glBegin(GL_LINES);

	for (int i=0;i<model.edges.size();i++)
	{
		Edge* edge=model.edges.at(i);

		if (edge->boarder==0)
		{
			continue;
		}

		Vertex *v1, *v2;

		v1=edge->vertices[0];
		v2=edge->vertices[1];

		glVertex3f(v1->position.x,v1->position.y,v1->position.z);

		glVertex3f(v2->position.x,v2->position.y,v2->position.z);
	}

	glEnd();*/
}

void InitCloth()
{
	model.InitCloth("test.obj");

	printf("Triangles: %d\n",model.triangles.size());
	printf("Edges: %d\n",model.edges.size());
	printf("Vertices: %d\n",model.vertices.size());

	for (int i=0;i<model.solver.scale;i++)
	{
		printf("@ %d\n",model.solver.sparse_M[i]->size());

		StiffChain* sc = model.solver.sparse_M[i];

		for (int j=0;j<sc->size();j++)
		{
			Stiff* sm = sc->at(j);

			printf("  - %d %d\n", sm->index[0], sm->index[1]);
		}


	}
}

#endif